//
//  main.cpp
//  引用
//
//  Created by linjinzhu on 2017/7/11.
//  Copyright © 2017年 linjinzhu. All rights reserved.
//

#include <iostream>

using namespace std;


int arr[3] = {1,2,3};

// 普通引用
void normal();

// 作为函数参数
void swapInt(int &a, int &b);

// 作为函数返回值
int & setValue(int i);
int & func();

int main(int argc, const char * argv[]) {
    normal();

    int a=1, b=3;
    cout << "交换前：a=" << a << "\tb=" << b << endl;

    swapInt(a, b);
    cout << "交换后：a=" << a << "\tb=" << b << endl;

    cout << "========" << endl;


    cout << "改变前" << endl;
    for (int i=0; i<3; i++) {
        cout << arr[i] << endl;
    }

    setValue(1) = 10;
    setValue(2) = 20;

    cout << "改变后" << endl;
    for (int i=0; i<3; i++) {
        cout << arr[i] << endl;
    }

    cout << "========" << endl;
    func() = 10;
    int r = func();
    cout << r << endl;
    r = 1;
    cout << func() << endl;

    return 0;
}

void normal()
{
    int x = 9;

    int &xx = x;

    cout << "实际值：" << x << endl;
    cout << "用引用访问值：" << xx << endl;

    xx = 10;

    cout << "实际值：" << x << endl;
    cout << "用引用访问值：" << xx << endl;
}

// 作为函数参数
void swapInt(int &a, int &b)
{
    int c = 0;
    c = a;
    a = b;
    b = c;
}

int & setValue(int i)
{
    return arr[i];
}

int & func()
{
    static int i;
    return i;
}
